/// @tags: Sieve DivBlock NumberTheory
#include <cstdio>
#include <iostream>

using namespace std;

namespace BlueQuantum {

typedef long long ll;

int const N = 4e6 + 5, B = 2e3, mod = 1e9 + 7;

int n, m, q, cnt;
int bel[N], l[B + 5], r[B + 5], pri[N / 8], phi[N];
bool vis[N];
ll val[N], sum[N], Sum[B + 5], f[N];

inline int getGCD(int a, int b) { return b ? getGCD(b, a % b) : a; }

inline ll qpow(ll base, ll exp) {
  ll res = 1;
  while (exp) {
    if (exp & 1) res = res * base % mod;
    base = base * base % mod;
    exp >>= 1;
  }
  return res;
}

inline ll query(int x) {
  if (!x) return 0;
  return (Sum[bel[x] - 1] + sum[x]) % mod;
}

inline void prework() {
  m = (n - 1) / B + 1;
  for (int i = 1; i <= m; ++i) l[i] = r[i - 1] + 1, r[i] = l[i] + B - 1;
  r[m] = n;
  for (int i = 1; i <= n; ++i) val[i] = (ll)i * i % mod;
  for (int i = 1; i <= m; ++i) {
    sum[l[i]] = val[l[i]], bel[l[i]] = i;
    for (int j = l[i] + 1; j <= r[i]; ++j) {
      bel[j] = i;
      if ((sum[j] = sum[j - 1] + val[j]) >= mod) sum[j] -= mod;
    }
    if ((Sum[i] = Sum[i - 1] + sum[r[i]]) >= mod) Sum[i] -= mod;
  }
  phi[1] = 1;
  for (int i = 2; i <= n; ++i) {
    if (!vis[i]) {
      pri[++cnt] = i;
      phi[i] = i - 1;
    }
    for (int j = 1; j <= cnt && pri[j] * i <= n; ++j) {
      vis[i * pri[j]] = true;
      if (i % pri[j] == 0) {
        phi[i * pri[j]] = phi[i] * pri[j];
        break;
      }
      phi[i * pri[j]] = phi[i] * (pri[j] - 1);
    }
  }
  for (int i = 1; i <= n; ++i)
    if ((f[i] = f[i - 1] + (ll)phi[i] * i % mod * i % mod) >= mod) f[i] -= mod;
}

inline int main() {
  cin >> q >> n;
  prework();
  for (int i = 1, a, b, k, gcd; i <= q; ++i) {
    ll x, Ans = 0;
    cin >> a >> b >> x >> k;
    x %= mod, gcd = getGCD(a, b);
    val[gcd] = x * gcd % mod * gcd % mod * qpow((ll)a * b % mod, mod - 2) % mod;
    for (int j = gcd; j <= r[bel[gcd]]; ++j)
      if ((sum[j] = (j > l[bel[gcd]] ? sum[j - 1] : 0) + val[j]) >= mod) sum[j] -= mod;
    for (int j = bel[gcd]; j <= m; ++j)
      if ((Sum[j] = Sum[j - 1] + sum[r[j]]) >= mod) Sum[j] -= mod;
    for (int l = 1, r; l <= k; l = r + 1) {
      r = k / (k / l);
      Ans = (Ans + f[k / l] * (query(r) - query(l - 1) + mod) % mod) % mod;
    }
    cout << Ans << '\n';
  }
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("P3700 [CQOI2017] 小Q的表格.in", "r", stdin);
  freopen("P3700 [CQOI2017] 小Q的表格.out", "w", stdout);
#endif
#endif

  ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  return BlueQuantum::main();
}
